blog

Home / DeveloperSection / Blogs / OOP (Object Oriented Programming)

OOP (Object Oriented Programming)

Royce Roy2046 21-Jan-2017

OOPs is a problem solvingmethodology. It decomposelarger program into smaller unit called classes. It is easy to use, modify

Object orientation- Focus will be an object rather than whole system.

Class- it is like a blueprint or template for its object. It wrap member function and data into single unit.it is the logical representation of some real world entity or object.

Object-It is an instance of the class/physical representation of the logic class. We can access the member of the class by its object

using System;           
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication6
{
    classemployee
    {
        publicint empno;
        publicstring empname;
    }
   
    classProgram
    {
 
        staticvoid Main(string[] args)
        {
            //to create object
            employee emp = newemployee();
            emp.empno = 1001;
            emp.empname = "Mindstick";
            Console.WriteLine(emp.empno);
            Console.WriteLine(emp.empname);
            Console.ReadKey();
        }
    }
}
 

Pillar of OOP:

1 Encapsulation- it is the feature of data hiding with help of access modifiers, so that it cannot by accessed by the outside world here outside world means outside the class.

  • Access modifiers
  • Public-accessible everywhere (in class where defined, child class by object)
  • Protected-In class in child class
  • Private-only in class
  • Default-only in class
  • Default-private

Encapsulation example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
                       
namespace ConsoleApplication6
{
    classemployee
    {
        publicint empno;
        protectedstring empname;
        privateint age;
    }
   
    classProgram
    {
 
        staticvoid Main(string[] args)
        {
            //to create object
            employee emp = newemployee();
            emp.empno = 1001;
            emp.empname = "Mindstick";// this line shows error
            emp.age = 20;// this line shows error due to protection level
            Console.WriteLine(emp.empno);
            Console.WriteLine(emp.empname);
            Console.ReadKey();
        }
    }
}
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication6
{
    classemployee
    {
        publicint empno;
        protectedstring empname;
        privateint age;
    }
    classsalary:employee
    {
        publicvoid display()
        {
            empno = 1002;
            empname = "Mindstick";
            age=10//error
        }
    }
   
    classProgram
    {
 
        staticvoid Main(string[] args)
        {
            //to create object
            employee emp = newemployee();
            emp.empno = 1001;
            emp.empname = "Mindstick";// this line shows error
            emp.age = 20;// this line shows error due to protection level
            Console.WriteLine(emp.empno);
            Console.WriteLine(emp.empname);
            Console.ReadKey();
        }
    }
}
 

For accesing private member ex.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication6
{
    classemployee
    {
                privateint age;
                publicvoid setage(int x)
                {
                    if (x >= 18 && x <= 100)
                    {
                        age = x;
                        Console.Write("age set!");
                    }
                    else
                        Console.WriteLine("age not set");
                }
    }
  
   
    classProgram
    {
 
        staticvoid Main(string[] args)
        {
            //to create object
            employee emp = newemployee();
            emp.setage(10);
               emp.setage(20);
            Console.ReadKey();
        }
    }
}


OOP (Object Oriented Programming)

Inheritance:

It is the feature of OOP .To inherited the property of one class into another class.

It support single inheritance, multi-level inheritance, hierarchical inheritance

Example to show inheritance

  classA              
    {                   
        publicint x;            }
    classB:A//single level
    {
        int y;
    }
    classc:B//multilevel
    {
        int c;
    }
    classD : A//heirarchical
    {
    }


You can also visit these related articles and blogs

Introduction to OOPS (Object oriented Programming System)

What is a information hiding in object oriented programming

What is difference between object oriented programming language and object based programming language?



Updated 17-Mar-2018

Leave Comment

Comments

Liked By